package com.kuyun.base.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.kuyun.base.base.Page;
import com.kuyun.base.mapper.BaseUserMapper;
import com.kuyun.base.model.BaseUser;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.service.BaseUserService;
import com.kuyun.base.util.Constants;
import com.kuyun.base.util.IDUtil;
import com.kuyun.base.util.MD5Code;

/**
 * 用户Service实现类
 * 
 * @author 史梁辰
 * @date 2016-6-19下午9:49:27
 */
@Service("baseUserService")
public class BaseUserServiceImpl implements BaseUserService {
	/**
	 * 用户数据接口对象
	 */
	@Resource
	private BaseUserMapper userMapper;

	/**
	 * 新增或修改用户 根据id判断
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午9:55:09
	 * @param User
	 * @return
	 * @throws Exception
	 */
	public boolean addOrUpdate(BaseUser user) throws Exception {
		int resultNum = 0;
		MD5Code md5 = new MD5Code();
		if (!StringUtils.isEmpty(user.getPassword())) {
			user.setPassword(md5.getMD5ofStr(user.getPassword()));
		}
		if (StringUtils.isEmpty(user.getId())) {// 判断id是否为空 为空则为新增 否则为修改
			user.setId(IDUtil.getId());
			user.setCreateTime(new Date());
			user.setCreateUserId("6bac7292b9354b25a6e8d0edbf0ee1c3");
			resultNum = userMapper.insertUser(user);
		} else {
			user.setUpdateTime(new Date());
			user.setUpdateUserId("6bac7292b9354b25a6e8d0edbf0ee1c3");
			resultNum = userMapper.updateUser(user);
		}
		if (resultNum > 0) {// 判断返回的受影响行数 如果大于0则有效 否则无效
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除用户
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午10:35:25
	 * @param User
	 * @return
	 * @throws Exception
	 */
	public HttpResult deleteUser(String id) throws Exception {
		HttpResult result = new HttpResult();
		int count = userMapper.getEmpCount(id);
		if (count > 0) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("该用户已绑定公司员工，无法删除");
			return result;
		}
		int resultNum = userMapper.deleteUser(id);
		int resultNum1 = userMapper.deleteUser_Role_ByUserId(id);

		if ((resultNum > 0 && resultNum1 > 0) || resultNum > 0) {// 判断返回的受影响行数
																	// 如果大于0则有效
																	// 否则无效
			result.setResultCode(Constants.RESULT_SUCCESS);
			result.setDesc("删除用户成功");
		} else {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("删除用户失败");
		}
		return result;
	}

	/**
	 * 查询用户数据 根据page对象判断查询全部还是分页查询
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午10:35:48
	 * @param page
	 * @param User
	 * @return
	 * @throws Exception
	 */
	public List<BaseUser> findUser(Page<BaseUser> page, BaseUser user) throws Exception {
		page.setPageNumber((page.getPageNumber() - 1) * page.getPageSize());
		return userMapper.findUsers(page, user);
	}

	/**
	 * 根据id获取用户
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午10:37:55
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public BaseUser getUserById(String id) throws Exception {
		return userMapper.getUserById(id);
	}

	public int getUserConunt(BaseUser user) throws Exception {
		return userMapper.getUserConunt(user);
	}

	public int checkUserName(String userName, String id) {
		return userMapper.checkUserName(userName, id);
	}

	/**
	 * 根据登录名查询用户信息
	 * 
	 * @author 唐靖
	 * @date 2016年7月14日下午6:24:26
	 * @param userName
	 * @return
	 */
	public BaseUser findUserByLogInName(String userName) {
		return userMapper.login(userName);
	}

	/**
	 * 获取用户所有的权限code
	 * 
	 * @author 唐靖
	 * @date 2016年7月14日下午6:26:47
	 * @param userName
	 * @return
	 */
	public Set<String> getAllPerByUserName(BaseUser user) {
		List<String> pris = userMapper.getAllPerByUserName(user);
		Set<String> pri = new HashSet<String>(pris);
		return pri;
	}
}
